#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/cdev.h>
#include <linux/types.h>
MODULE_LICENSE("GPL");
struct cddev{
	struct cdev xdev;
	char ringbuf[20];
	dev_t devid;
};
struct cddev devs[2] = {
	{
		.ringbuf = "AAAAAAAAAAAAAA",
		.devid = MKDEV(88,99),
	},
	{
		.ringbuf = "BBBBBBBBBBBBBBBBBBB",
		.devid = MKDEV(88,100),
	}
};



ssize_t xread(struct file *fp,char __user *buf,size_t len ,loff_t *off)
{
	struct cddev *xptr = container_of(fp->private_data,struct cddev ,xdev);
	strncpy(buf ,xptr->ringbuf,len);
	return len;
}

ssize_t xwrite(struct file *fp,const char __user *buf,size_t len ,loff_t *off)
{
	int i = 0;
	for(i = 0;i < 2; i++)
	{
		if(fp->private_data == &devs[i].xdev)
		{
			strncpy(devs[i].ringbuf,buf,len);
		}
	}
	return len;
}

int xopen(struct inode *no,struct file*fp)
{
	fp->private_data = no->i_cdev;
	printk("xopen\n");
	return 0;
}
int xrelease(struct inode *no,struct file *fp)
{
	printk("xrelease\n");
	return 0;
}

struct cdev xdev;
struct file_operations fops = {
	.open = xopen,
	.read = xread,
	.write = xwrite,
	.release = xrelease,
};

int chadev_init(void)
{
	int i = 0;
	for(i = 0;i < 2 ;i++)
	{
		cdev_init(&devs[i].xdev,&fops);
		cdev_add(&devs[i].xdev,devs[i].devid,1);
	}
		return 0;
}


void chadev_exit(void)
{
	int i = 0;
	for(i =0 ;i< 2;i++)
	{
		cdev_del(&devs[i].xdev);
		printk("bay\n");
	}
}

module_init(chadev_init);
module_exit(chadev_exit);
